package com.xwltz.controller;

import com.xwltz.entity.Student;
import com.xwltz.service.StudentService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Resource
    private RedisTemplate redisTemplate;

    private final StudentService service;

    public StudentController(StudentService service) {
        this.service = service;
    }

    @GetMapping("/list")
    public List<Student> list() {
        List<Student> studentList;
        if (redisTemplate.opsForValue().get("list") == null) {
            studentList = service.findAll();
            redisTemplate.opsForValue().set("list", studentList);
        } else {
            studentList = (List<Student>) redisTemplate.opsForValue().get("list");
        }
        return studentList;
    }

    @GetMapping("/get/{id}")
    public Student getById(@PathVariable("id") long id) {
        return service.findById(id);
    }
}
